package com.umeox.mediaplayer

import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.widget.ImageView
import androidx.core.app.ActivityCompat
import com.bumptech.glide.GlideBuilder
import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.load.engine.cache.DiskLruCacheFactory
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.module.AppGlideModule
import com.bumptech.glide.request.RequestOptions
import java.io.File


const val KEY_URI_VIDEO = "videoUrI"
const val KEY_URI_MUSIC = "musicUrI"



@GlideModule
class GlideModule : AppGlideModule() {
    override fun applyOptions(context: Context, builder: GlideBuilder) {

        val diskCacheSizeBytes = 1024 * 1024 * 280L
        val externalFilesDir = context.getExternalFilesDir(null)
        val file = File(externalFilesDir, "AppImage")
        if (!file.exists()) {
            file.mkdirs()
        }
        builder.setDiskCache(
            DiskLruCacheFactory(file.absolutePath, diskCacheSizeBytes)
        )

    }

}

var circleRequestOptions = RequestOptions.bitmapTransform(RoundedCorners(8)).override(120, 80).centerCrop()

fun ImageView.load(any: Any) {

    GlideApp.with(this.context).load(any).into(this)
}

fun ImageView.loadImageCorner(any: Any?, placeholder: Int = R.drawable.item_movie_placeholder) {

    GlideApp.with(this.context).load(any)
        .apply(circleRequestOptions.placeholder(placeholder).error(placeholder)).into(this)
}

fun checkPermission(activity: Activity?, permissions: Array<String?>, requesCode: Int): Boolean {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        val result = ActivityCompat.checkSelfPermission(activity!!, permissions[0]!!)
        if (result != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(
                activity,
                arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
                requesCode
            )
            return false
        }
    }
    return true
}

